import 'dart:math';

import 'package:decimal/decimal.dart';
import 'package:lib_uikit/widgets/chart/chart_xy_value_utils.dart';
import 'package:test/test.dart';

void main() {
  group('ChartXYValueUtils', () {
    test('calculateYIntervalV2', () {
      void check(double maxSpaceValue, int internalCount, double expectedValue) {
        final value = ChartXYValueUtils.calculateYIntervalV2(
          Decimal.parse(maxSpaceValue.toString()),
          Decimal.fromInt(internalCount),
        );
        expect(value.toDouble(), expectedValue);
      }

      check(2, 5, 0.4);
      check(18721, 5, 4000.0);
      check(127836.0, 5, 30000);
      check(1278367821637.0, 5, 300000000000);
      check(0.012, 5, 0.003);
      check(0.0012, 5, 0.0003);
      check(0.8129378912, 5, 0.2);
      check(0.000127178, 5, 0.00003);
      check(0.005, 5, 0.001);
      check(0.05, 5, 0.01);
      check(0.5, 5, 0.1);
      check(5, 5, 1);
      check(500.0, 5, 100);
      check(5000.0, 5, 1000);
    });

    test('calculateYIntervalV2 - 测试极大或极小数值情况', () {
      expect(
        ChartXYValueUtils.calculateYIntervalV2(Decimal.parse('19642268694050234000.0'), Decimal.fromInt(5)),
        Decimal.parse('4000000000000000000'),
      );

      expect(
        ChartXYValueUtils.calculateYIntervalV2(Decimal.parse('0.000000000000000021378'), Decimal.fromInt(5)),
        Decimal.parse('0.000000000000000005'),
      );

      expect(
        ChartXYValueUtils.calculateYIntervalV2(Decimal.parse('0.000000000000000000128717'), Decimal.fromInt(5)),
        Decimal.parse('0.00000000000000000003'),
      );

      expect(
        ChartXYValueUtils.calculateYIntervalV2(Decimal.parse('0.0000000000000000002417'), Decimal.fromInt(5)),
        Decimal.parse('0.00000000000000000005'),
      );
    });
  });

  // test('calculateYIntervalV2 - 常规小数值与calculateYInterval的一致性', () {
  //   // 常规数值
  //   checkCalculatedResults(2, 5);
  //   checkCalculatedResults(18721, 5);
  //   checkCalculatedResults(127836, 5);
  //   checkCalculatedResults(1278367821637, 5);

  //   checkCalculatedResults(0.012, 5);
  //   checkCalculatedResults(0.0012, 5);
  //   checkCalculatedResults(0.8129378912, 5);
  //   checkCalculatedResults(0.000127178, 5);

  //   checkCalculatedResults(0.005, 5);
  //   checkCalculatedResults(0.05, 5);
  //   checkCalculatedResults(0.5, 5);
  //   checkCalculatedResults(5, 5);
  //   checkCalculatedResults(500, 5);
  //   checkCalculatedResults(5000, 5);

  //   /// 测试小于1的数值
  //   for (var i = 0; i < 100000; i++) {
  //     final value = Decimal.fromInt(Random().nextInt(1000000000)) * Decimal.parse('0.000000001');
  //     checkCalculatedResults(value.toDouble(), 5);
  //   }

  //   /// 测试大于1的数值
  //   for (var i = 0; i < 100000; i++) {
  //     final value = Decimal.fromInt(Random().nextInt(1000000000));
  //     checkCalculatedResults(value.toDouble(), 5);
  //   }
  // });
}

// void checkCalculatedResults(double maxSpaceValue, int internalCount) {
//   final value1 = ChartXYValueUtils.calculateYInterval(
//     maxSpaceValue,
//     internalCount,
//   );
//   final value2 = ChartXYValueUtils.calculateYIntervalV2(
//     Decimal.parse(maxSpaceValue.toString()),
//     Decimal.fromInt(internalCount),
//   );
//   expect(value1, value2.toDouble());
// }
